package de.koller.worlders.gameplay.inventory;

import de.koller.oceanFramework.glyph.GlyphRendererGL;
import de.koller.oceanFramework.glyph.GlyphScreen;
import de.koller.oceanFramework.glyph.GlyphScreenGL;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.tree.Mother;
import de.koller.worlders.WSea;
import de.koller.worlders.WorldersRoot;
import de.koller.worlders.gameplay.resource.FontValue;
import de.koller.worlders.render.IGLRender;
import org.lwjgl.opengl.GL11;

public class InventoryV extends WSea implements IGLRender {
	
	@Mother private Inventory inventory;	
	@Child private final GlyphScreenGL gscreen = new GlyphScreenGL( 512, 512,
			new FontValue(), "de/koller/worlders/gameplay/resource/font.png" );
	private int x, y = 12, gw = 30, gh = 10;

	public InventoryV() {
		super();
	}

	@Override
	protected void init() {
		super.init();
		x = WorldersRoot.D_WIDTH - (gw+2) * GlyphScreen.GLYPH_WIDTH;
				
		wroot.rendering.addGUIElement( this );
	}

	@Override
	protected void release() {
		super.release();
		
		wroot.rendering.removeGUIElement( this );
	}

	@Override
	public void renderGL( int elapsedTime ) {	
		if( !inventory.isVisible() )
			return;
		
		gscreen.getRenderer().setCurrentBackColor( 0x00000000 );
		gscreen.getRenderer().setCurrentFontColor( 0xffffffff );
		gscreen.getRenderer().setCurrentSecColor( 0xff0000ff );
	
		GlyphRendererGL r = gscreen.getRenderer();
		r.renderGlyph( 0, 0, 2, 7 );
		r.renderGlyph( 1, 0, 3, 7 );
		r.renderString( 2, 0, "INVENTORY" );
		for( int i = 11; i < gw; i++ )
			r.renderGlyph( i, 0, 3, 7 );
		for( int i = 1; i < gh; i++ ) {
			r.renderGlyph( 0, i, 3, 8 );
			r.renderGlyph( gw, i, 3, 8 );
		}
		for( int i = 1; i < gw; i++ )
			r.renderGlyph( i, gh, 3, 7 );
		r.renderGlyph( gw, 0, 4, 7 );
		r.renderGlyph( 0, gh, 2, 8 );
		r.renderGlyph( gw, gh, 4, 8 );
		
		for( int i = 0; i < inventory.getContent().size(); i++ )
			renderEntity( i );
		
		GL11.glColor3f( 1, 1, 1 );
		
		gscreen.updateBuffer();
		gscreen.getTexture().draw2D( x, y, 512, 512, 20 );
	}

	private void renderEntity( int index ) {
		InventoryEntity e = inventory.getContent().get( index );
		GlyphRendererGL r = gscreen.getRenderer();
		int i = index*2 + 1; 
		
		r.renderString( 5, i, e.getItem().getTitle() );
		e.getItem().getTexture().draw2D(	x + GlyphScreen.GLYPH_WIDTH*1, 
											y + GlyphScreen.GLYPH_HEIGHT*(i), 
											24, 24, 20 );
		
		String n = e.getNumber() + "x";
		r.renderString( gw - n.length(), i, n );
	}
	
}
